package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {


	@Autowired
	ShoppingCartMapper shoppingCartMapper;

	@Autowired
	DishMapper dishMapper;
	@Autowired
	SetmealMapper setmealMapper;
	/**
	 * 添加购物车
	 * @param shoppingCartDTO
	 */
	@Override
	public void addshoppingCart(ShoppingCartDTO shoppingCartDTO) {
		//数据准备，封装ShoppingCart
		ShoppingCart shoppingCart = new ShoppingCart();
		BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
		//获得userId
		shoppingCart.setUserId( BaseContext.getCurrentId());
		//获得购物车数据集合
		List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
		//判断当前加入的购物车中的商品是否存在
		if (list != null && list.size() > 0) {
			//如果存在，则需要将数量加1
			ShoppingCart cart = list.get(0);
			 cart.setNumber(cart.getNumber() + 1);
			 shoppingCartMapper.updateNumberById(cart);
		}else {
			//如果不存在，则插入一条购物车数据
			//判断添加菜品还是套餐
			Long dishId = shoppingCartDTO.getDishId();
			if(dishId != null) {
				//本次添加的为菜品
				//数据封装
				Dish dish = dishMapper.getByid(dishId);
				shoppingCart.setName(dish.getName());
				shoppingCart.setImage(dish.getImage());
				shoppingCart.setAmount(dish.getPrice());
			}else{
				//本次添加为套餐
				//数据封装
				Long setmealId = shoppingCartDTO.getSetmealId();
				Setmeal setmeal = setmealMapper.getById(setmealId);
				shoppingCart.setName(setmeal.getName());
				shoppingCart.setImage(setmeal.getImage());
				shoppingCart.setAmount(setmeal.getPrice());
			}
			//统一添加数据
			shoppingCart.setNumber(1);
			shoppingCart.setCreateTime(LocalDateTime.now());
			shoppingCartMapper.insert(shoppingCart);
		}
	}

	/**
	 * 获取当前用户的购物车消息
	 * @return
	 */
	@Override
	public List<ShoppingCart> list() {
		//数据准备
		ShoppingCart shoppingCart=new ShoppingCart();
		shoppingCart.setUserId(BaseContext.getCurrentId());
		return shoppingCartMapper.list(shoppingCart);
	}

	/**
	 * 请空购物车
	 */
	@Override
	public void clean() {
		//获取用户id
		Long userId = BaseContext.getCurrentId();
		shoppingCartMapper.deleteByUserId(userId);
	}

	/**
	 * 购物车删除一个商品
	 */
	@Override
	public void sub(ShoppingCartDTO shoppingCartDTO) {
		ShoppingCart shoppingCart = new ShoppingCart();
		BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
		shoppingCart.setUserId(BaseContext.getCurrentId());
		//得到一条购物车数据集合
		List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
		//判断集合是否为空
		if (list != null && list.size() > 0) {
			ShoppingCart cart = list.get(0);
			//判断份数是否为1
			if(cart.getNumber() == 1) {
				//为1则删除该条数据
				shoppingCartMapper.deleteById(cart.getId());
			}else{
				//不为1则减1
				cart.setNumber(cart.getNumber() - 1);
				shoppingCartMapper.updateNumberById(cart);
			}
		}



	}
}
